package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDto;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 菜品列表
     * @param categoryId
     * @return
     */

    @GetMapping("/list")
    public R<List<DishDto>> dishlist(String categoryId ,String status){
        log.info("------下拉分类----");
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId)
                .eq(Dish::getStatus,status);
        List<Dish> dishes = dishService.list(queryWrapper);
        //新建一个Dto类 并将查询结果传入;
        List<DishDto> dishDtos = new ArrayList<>();
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            //找到当前dish对应的id
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper1);
            DishDto dishDto = new DishDto();
            //复制到新的dto类
            BeanUtils.copyProperties(dish,dishDto);
          //  BeanUtils.copyProperties(dishFlavorList,dishDto.getFlavors());
            dishDto.setFlavors(dishFlavorList);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }



}
